package com.lvcoding.cloud.user.conroller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * @Description 描述
 * @Date 2020-07-28 6:03 下午
 * @Author wuyanshen
 */
@Controller
@RequestMapping("img")
public class MinioController {

    //显示图片
    @SneakyThrows
    @GetMapping("{name}")
    public void getImg(@PathVariable("name")String name, HttpServletResponse response) {
        MinioClient minioClient = new MinioClient("https://play.min.io", "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
        InputStream inputStream = minioClient.getObject("asiatrip", name);
        IoUtil.copy(inputStream, response.getOutputStream());
    }

    //上传图片
    @SneakyThrows
    @GetMapping("/upload")
    public void upload(@RequestParam("filePath")String filePath) {
        String fileName = IdUtil.simpleUUID();
        System.out.println("fileName = " + fileName);
        MinioClient minioClient = new MinioClient("https://play.min.io", "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
        minioClient.putObject("asiatrip", fileName, filePath);
    }

    @GetMapping("index")
    public String index(){
        return "index";
    }
}
